// pages/find/find.js
const app = getApp();
const util = require('../../utils/util.js');
// con//dfgfdgdfgfksdjgkdfsjgkdfssdjkgjsdklgsd
const url = require("../../utils/request.js").commonUrl;
const request = require("../../utils/request.js").request;

const collectRequest = require("../../utils/util.js").collectRequest;
const collectUtil = require("../../utils/collect-util.js");
const size = 4;
let num=1;
let moreEnable,refreshEnable;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        url: url,
        windowWidth: "",
        list: [],
        index: "",
        empty: "",
        hasMore: true
    },
    //收藏活动点
    like: function (event) {
        let that = this;
        util.isLogin(function () {
            let index = event.currentTarget.dataset.itemid;
            let value = !that.data.list[index].interest;
            let param = {}, result = [];
            let interestParam = {}, countParam = {};
            // param[key] = value;

            const params = {
                "activityId": that.data.list[index].activityId, //活动点ID
                "weixin": app.globalData.openid,  //用户ID
                "isInterest": value//是否收藏，true为收藏，false为取消收藏
            };

            collectRequest(params, function (isInterest, count) {
                // that.setData(param);
                let isInterestKey = 'list[' + index + '].interest';
                let isInterestValue = isInterest;
                let countKey = 'list[' + index + '].numberOfInterest';
                let countVal = count;

                interestParam[isInterestKey] = isInterestValue;
                countParam[countKey] = countVal;
                //log(isInterest, count);

                that.setData(interestParam);
                that.setData(countParam);

                // result.push(isInterestValue);
                // result.push(countVal);
                // collectUtil.collectActivity(that.data.list[index].activityId, result);
            });

            app.globalData.flag = that.data.list[index].activityId;
        });


    },

    //进入详情页
    detailGo: function (event) {
        var that = this;
        let index = event.currentTarget.dataset.itemid;
        let activityId = this.data.list[index].activityId;
        let activityAreaId = this.data.list[index].activityAreaId;
        var detailInfo = {
            activityId: activityId,
            activityAreaId: activityAreaId
        }
        that.setData({
            index: index
        });

        wx.navigateTo({
            url: '../details/details?detailInfo=' + JSON.stringify(detailInfo),
            success: function (res) {
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },
    //获取活动列表
    getList: function (params, type) {
        let that = this;
        if (type == 1) {
            if (!refreshEnable) {
                return
            } else {
                refreshEnable = false;
                moreEnable=false;
            }
            wx.showNavigationBarLoading();
            if (!that.data.hasMore) {
                that.setData({
                    hasMore: true
                })
            }
            request('/m/discover/activity.json', params).then(function (res) {
                refreshEnable = true;
                moreEnable=true;
                wx.hideNavigationBarLoading();
                if (!res.data.result) {
                    res.data.result = [];
                }
                //console.log("res", res.data.result)

                if (res.data.result.length < size) {
                    that.setData({
                        list: res.data.result,
                        hasMore: false
                    })
                } else {
                    ++num;
                    that.setData({
                        list: res.data.result,
                        hasMore: true
                    })
                }

                that.addCollectBean();
            })
             
           
        }
        else if(type==2){
            if (!moreEnable){
                return
            }else{
                moreEnable=false;
            }
            if (!that.data.hasMore) {
                return
            }
            request('/m/discover/activity.json', params).then(function (res) {
                moreEnable = true;
                if (!res.data.result) {
                    res.data.result = [];
                }

                if (res.data.result.length < size) {
                    that.setData({
                        hasMore: false,
                        list: that.data.list.concat(res.data.result)
                    })
                } else {
                    ++num
                    that.setData({
                        list: that.data.list.concat(res.data.result),
                        hasMore: true
                    })

                }

                that.addCollectBean();

            })
        }else{
            request('/m/discover/activity.json', params).then(function (res) {
                refreshEnable=true;
                moreEnable=true;
                if (res.data.result.length < size) {
                    that.setData({
                        list: res.data.result,
                        hasMore: false
                    })
                } else {
                    ++num;
                    that.setData({
                        list: res.data.result,
                        hasMore: true
                    })
                }

                that.addCollectBean();
            })
        }
    },
    //收藏活动
    addCollectBean: function () {
        let that = this;
        for (let i = 0; i < that.data.list.length; i++) {
            let collectObsBean = new collectUtil.CollectObsBean();
            // console.log(collectObsBean.Tag);
            collectObsBean.tag = collectUtil.Tag.near;
            collectObsBean.activityId = that.data.list[i].activityId;

            collectObsBean.callBack = function CallBack(result) {
                that.data.list[i]["interest"] = result[0];
                that.data.list[i]["numberOfInterest"] = result[1];
                //TODO 改成局部更新
                that.setData(that.data);
                //console.log("addCollectBean", i, that.data.list[i],result);
            };
            collectUtil.addActivity(collectObsBean);
        }
    },
    //下拉刷新
    refresh: function () {
        num=1;
        const params = {
            pageNum: 1,
            pageSize: size,
            id: "",
            weixin: app.globalData.openid
        }
        this.getList(params, 1);

    },
    //上拉加载
    loadMore: function () {
        const params = {
            pageNum:num,
            pageSize: size,
            id: "",
            weixin: app.globalData.openid

        }
        this.getList(params,2)
    },
    //日期格式化
    formatDate: function (nS) {

        //1509430475616

        //  return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

        return new Date(parseInt(nS)).toLocaleString().substr(0, 10) //2010/12/23

        //  return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, '-');//2010/12/23 上午10:05

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        const params = {
            pageNum: 1,
            pageSize: size,
            id: "",
            weixin: app.globalData.openid
        }
        this.getList(params);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    windowWidth: res.windowWidth
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    }
})